import 'package:flutter_bili_app/db/hi_cache.dart';
import 'package:flutter_bili_app/http/dao/login_dao.dart';

enum HttpMethod { GET, POST, DELETE }

///基础请求
abstract class BaseRequest {
  var pathParams;
  var isHttps = true;

  String baseUrl() {
    return "api.devio.org";
  }

  String path();

  HttpMethod httpMethod();

  String url() {
    Uri uri;
    var pathStr = path();

    ///拼接path参数
    if (pathParams != null) {
      if (path().endsWith("/")) {
        pathStr = "${path()}$pathParams";
      } else {
        pathStr = "${path()}/$pathParams";
      }
    }

    ///判定http或https方法
    if (isHttps) {
      uri = Uri.https(baseUrl(), pathStr, params);
    } else {
      uri = Uri.http(baseUrl(), pathStr, params);
    }
    print(uri.toString());

    ///增加登录后鉴权功能
    if (needLogin() == true) {
      addHeader(LoginDao.BOARDING_PASS,
          HiCache.getInstance().getValue(LoginDao.BOARDING_PASS));
    }
    return uri.toString();
  }

  bool needLogin();

  ///添加参数
  Map<String, String> params = Map();

  BaseRequest add(String k, Object v) {
    params[k] = v.toString();
    return this;
  }

  ///添加header
  Map<String, dynamic> header = {
    "course-flag": "fa",
    "auth-token": "ZmEtMjAyMS0wNC0xMiAyMToyMjoyMC1mYQ==fa"
  };

  BaseRequest addHeader(String k, Object v) {
    header[k] = v;
    return this;
  }
}
